package com.naizhuai.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;

import com.naizhuai.ui.components.RadarData;
import com.naizhuai.ui.components.RadarView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



/**
 * Created by chqiu on 2016/9/5.
 */
public class AnimeUtil {
    private WeakReference<RadarView> mWeakRadarView;
    private HashMap<RadarData, ValueAnimator> mAnimes;
    private ValueAnimator anime;

    public AnimeUtil(RadarView view) {
        mWeakRadarView = new WeakReference<>(view);
        mAnimes = new HashMap<>();
    }

    public void animeValue(AnimeType type, int duration, RadarData data) {
        switch (type) {
            case ZOOM:
                startZoomAnime(duration, data);
                break;
        }
    }

    public boolean isPlaying() {
        boolean isPlaying = false;
        for (ValueAnimator anime : mAnimes.values()) {
            isPlaying = anime.isStarted();
            if (isPlaying) {
                break;
            }
        }
        return isPlaying;
    }

    public boolean isPlaying(RadarData data) {

        ValueAnimator anime = mAnimes.get(data);
        return anime != null && anime.isStarted();
    }

    public void clearAnimas(){
        if(mAnimes!=null)
            mAnimes.clear();
    }

    private void startZoomAnime(final int duration, final RadarData data) {
        anime = ValueAnimator.ofFloat(0, 1f);
        List<Float> values = data.getValue();
        List<Float> values2 = new ArrayList<>(values);
        anime.addUpdateListener(new AnimationUpdateListener(values,values2));
        anime.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                mAnimes.remove(data);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mAnimes.remove(data);
            }
        });
        anime.setDuration(duration).start();
        mAnimes.put(data, anime);
    }

    class AnimationUpdateListener implements ValueAnimator.AnimatorUpdateListener {

        private List<Float> values;
        private List<Float> values2;

        public AnimationUpdateListener(List<Float> values, List<Float> values2) {
            this.values = values;
            this.values2 = values2;
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            RadarView view = mWeakRadarView.get();
            if (view == null) {
                anime.end();
            } else {
                float percent = Float.parseFloat(animation.getAnimatedValue().toString());
                for (int i = 0; i < values.size(); i++) {
                    values.set(i, values2.get(i) * percent);
                }
                view.invalidate();
            }
        }
    }

    public enum AnimeType {
        ZOOM, ROTATE
    }
}
